home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / embedded / mon.c < prev    next >
C/C++ Source or Header  |  1994-10-20  |  7KB  |  497 lines

  1.  
  2. /*
  3.     Copyright 1990 Compouter Design Solutions Inc.
  4.     This code may be used by anyone in anyway.
  5.     CDS Inc. grants full rights to code to all users.
  6.     
  7.     Computer Design Solutions Inc.
  8.     P.O. Box 127
  9.     Statesville NC    28677
  10.     704-876-2346    Fax 704-876-2348
  11.  
  12. */
  13.  
  14.  
  15. #define TRUE    1
  16. #define FALSE    0
  17. #define CR     0x0d
  18.  
  19. /*    Flag definations    */
  20. #define OK    0
  21. #define    NODATA    1        /* Returns from get_v when no data found */
  22.  
  23.  
  24. static unsigned int start,end,value,last;
  25. static unsigned char temp[16];
  26. static unsigned char cin,flag;
  27. static unsigned char *p;
  28.  
  29. main()
  30.     {
  31. /*
  32.     Sign on and say hello
  33. */
  34.  
  35.     crlf();
  36.     print("CDS Small Monitor Version 1.0");
  37.     start=end=value=last=0;    /* init vars    */
  38.  
  39. /*    
  40.     Loop forever or until control C
  41.  
  42. */
  43.  
  44.     while(1)
  45.  
  46.     {
  47.  
  48. /*    
  49.     Get Command
  50. */
  51.  
  52.     crlf();
  53.     putchar('>');
  54.     cin=toupper(getchar());
  55.  
  56.     switch(cin)
  57.     {    
  58.     case 'F':            /* Fill Memory */
  59.         f_mem();
  60.         break;
  61.     case 'D':            /* Dump Memory */
  62.         d_mem();
  63.         break;    
  64.     case 'E':            /* Examine Memory */
  65.         e_mem();
  66.         break;
  67.     case 3:                /* Ctl C  Exit    */
  68.         exit_mon();
  69.     default:
  70.         break;
  71.  
  72.     }
  73.  
  74.     }
  75.     
  76.  
  77.     }
  78.  
  79. /*    Not normally used. This is for testing on a CPM machine */
  80.  
  81. exit_mon()
  82.     {
  83. #asm
  84.     jp    0
  85. #endasm
  86.     }
  87.  
  88. /*     Examine memory    */
  89.  
  90. e_mem()
  91.     {
  92.     print("xamine  ");
  93. /*
  94.     Get address
  95. */
  96.     start=get_v();
  97.     cin=' ';
  98.     p=start;
  99.     flag=OK;
  100.  
  101. /*
  102.      Loop till CR
  103. */
  104.  
  105.     while(flag==OK)
  106.         {
  107.         crlf();
  108. /*
  109.     Print address
  110. */
  111.  
  112.         phex(p,4,2);
  113.         print("=  ");
  114. /*
  115.     Print contents
  116. */
  117.  
  118.         phex(*p,2,2);
  119. /*
  120.     Get new value
  121. */
  122.  
  123.         value=get_v();
  124.         *p=value;
  125. /*
  126.     Next address
  127. */
  128.  
  129.         p++;
  130.         }
  131.     }
  132.  
  133. /*    Fill memory     */
  134. f_mem()
  135.     {
  136.     print("ill ");
  137. /*
  138.     Get start, end, and data pattern
  139. */
  140.  
  141.     start=get_v();
  142.     if(flag==NODATA)
  143.         fer();
  144.     end=get_v();
  145.     if(flag==NODATA)
  146.         fer();
  147.     value=get_v();
  148.     rcheck();
  149.     fill(start,end,value);
  150.     }
  151.  
  152. fer()
  153.     {
  154.     error("Fill needs more data");
  155.     }
  156.  
  157.  
  158. /*    Dump memory    */
  159. d_mem()
  160.     {
  161.     clear();
  162.     print("ump ");
  163. /*
  164.     Get Start and end
  165. */
  166.  
  167.     start=get_v();
  168.     if(cin!=CR)
  169.     end=get_v();
  170.     crlf();
  171.     rcheck();
  172.     dump(start,end);
  173.     }
  174.  
  175.  
  176.  
  177. /*    Error processing    */
  178.  
  179. error(s)
  180.     char *s;
  181.     {
  182.     crlf();
  183.     bell();
  184.     print(s);
  185.     crlf();
  186.     main();            /* sort of silly but works    */
  187.     }
  188.  
  189. /*    Ring the bell        */
  190. bell()
  191.     {
  192.     putchar(7);
  193.     }
  194.  
  195.  
  196. /*    Range check start to end    */
  197.  
  198. rcheck()
  199.     {
  200.     if(end<=start)
  201.         end=start+16;
  202.     }
  203. /*    Clear start,end,value,cin    */
  204. clear()
  205.     {
  206.     start=end=value=0;
  207.     cin=0;
  208.     }
  209.  
  210. /* get a hexadecimal value from the keyboard    */
  211.  
  212. get_v()
  213.     {
  214.     unsigned int val;
  215.     unsigned char cnt;
  216.     cnt=cin=0;
  217.     while((cin!=CR) && (cin!=','))
  218.     {
  219.     cin=getchar();
  220.     if(cnt==0) flag=NODATA; else flag=OK;
  221.     temp[cnt++]=cin;
  222.     if(cnt>5)
  223.         {
  224.         bell();
  225.         cnt--;
  226.         }
  227.  
  228.     /*    Back space occured    */
  229.  
  230.     if(cin=='\b')
  231.         {
  232.         if(cnt>0) cnt--; else bell();
  233.         print(" \b");
  234.         }
  235.  
  236.     }
  237.     temp[--cnt]=0;
  238.     upcase(temp);    
  239.     val=htob(temp,16);
  240.     if(flag==NODATA) return(last);
  241.     last=val;
  242.     return(val);
  243.     }
  244.  
  245.  
  246. /*     Fill from ad1 to ad2 with data                */
  247.  
  248. fill(start,end,data)
  249.     unsigned int start,end;
  250.     unsigned char data;
  251.     {
  252.     char *p;
  253.     for(p=start;p<end;)
  254.         *p++=data;
  255.     }
  256.  
  257.  
  258.  
  259. /*    Hex dump with address on left and ASCII on right    */
  260. /*    Start is the begin address and end is the end        */
  261.  
  262. dump(start,end)
  263.     int start,end;
  264.  
  265.     {
  266.     int c_cnt;
  267.     crlf();
  268.     for (p=start;p<end;)
  269.     {
  270.                     /* Print the address */
  271.     phex(p,4,4);
  272.     for(c_cnt=0;c_cnt<16;c_cnt++)
  273.     {
  274.     phex(*p++,2,1);
  275.     }
  276.     p-=16;                /* reset */
  277.                     /* Print the ASCII    */
  278.     for(c_cnt=0;c_cnt<16;c_cnt++)
  279.     {
  280.     pascii(*p++,'.',0);
  281.     }    
  282.     crlf();
  283.     }
  284.     }
  285.  
  286.  
  287.  
  288.     /*    Print a new line seq.    */
  289.  
  290. crlf()
  291.     {
  292.     print("\n\r");
  293.     }
  294.  
  295.  
  296. /*    rept will print char. s n times     */
  297.  
  298. rept(s,n)
  299.     char s,n;
  300.     {
  301.     if(n==0) return;
  302.     while(n--)
  303.         putchar(s);
  304.     }
  305.  
  306.  
  307. /* pascii(char,fill,spc)
  308.     print ascii or fill and spc traiing spaces
  309. */
  310.  
  311. pascii(chr,fill,spc)
  312.     unsigned char chr,fill,spc;
  313.     {
  314.     if(chr<' ' || chr>'}') 
  315.     putchar(fill);
  316.     else
  317.     putchar(chr);
  318.     space(spc);
  319.     }
  320.  
  321.  
  322.  
  323. /* phex(n,len,spc)
  324.     print in hex the number (n) 0 filled of len with spc trailing
  325.     spaces. phex(100,4,2) will print 0064  .
  326. */
  327.  
  328. phex(n,lgth,spc)
  329.     unsigned int n;
  330.     unsigned char lgth,spc;
  331.  
  332.     {
  333.  
  334.     itob(n,temp,16);
  335.     rept('0',(lgth-len(temp)));    /* 0 fill    */
  336.     print(temp);
  337.     space(spc);            /* print sep.     */
  338.                     /* Print the 16 hex digits */
  339.     }
  340.     
  341.  
  342.  
  343. /* String Print    */
  344.  
  345. print(s)
  346.     char *s;
  347.     {
  348.     while(*s)
  349.     putchar(*s++);
  350.     }
  351.  
  352.  
  353.     /*     space(x)   will print x spaces    */
  354.  
  355. space(x)
  356.     char x;
  357.     {
  358.     if(x==0) return;
  359.     while (x--)
  360.     putchar(' ');
  361.     }
  362.  
  363.  
  364.     /* This is where you put your char. out
  365.        code. The CDS Z-80/Z-280 compiler passes
  366.        the char in L. */
  367.  
  368. putchar(s)
  369.     char s;
  370.     {
  371.  
  372. #asm
  373.     push    bc
  374.     push    ix
  375.     ld    e,l
  376.     ld    c,2
  377.     call    5
  378.     pop    ix
  379.     pop    bc
  380. #endasm
  381.  
  382.     }
  383.  
  384.     /* get a charector from the keyboard    */
  385.  
  386. getchar()
  387.     {
  388. #asm
  389.     push    bc
  390.     push    ix
  391.     ld    c,1
  392.     call    5
  393. ;    and    a,5fh        ; force upper case
  394.     ld    l,a
  395.     pop    ix
  396.     ld    h,0
  397.     pop    bc
  398. #endasm
  399.     }
  400.     
  401. /* len returns the length of a string */
  402.  
  403. len(s)
  404.     char *s;
  405.     {
  406.     char i;
  407.     i=0;
  408.     while(*s++)
  409.     i++;
  410.     return i;
  411.     }
  412.  
  413.  
  414.  
  415. upcase(str)                     /* convert a string to uppercase */
  416.    char *str;
  417. {
  418.    char *start;
  419.    start = str;
  420.    while (*str) {
  421.       if (islower(*str))
  422.          *str = toupper(*str);
  423.       str++; }
  424.    return(start);
  425. }
  426.  
  427. /* itob -- convert an integer to a string in any base (2-36) 
  428.    where
  429.     n is the number to convert
  430.     s is a temp. string
  431.     base is the number base
  432. */
  433.  
  434. itob(n, s, base)
  435. char *s;
  436.     {
  437.     unsigned int u;
  438.     char *p, *q;
  439.     int negative, c;
  440.  
  441.     if (n < 0 && base == -10) {
  442.         negative = TRUE;
  443.         u = -n;
  444.         }
  445.     else {
  446.         negative = FALSE;
  447.         u = n;
  448.         }
  449.     if (base == -10)        /* signals signed conversion */
  450.         base = 10;
  451.     p = q = s;
  452.     do {                /* generate digits in reverse order */
  453.         if ((*p = u % base + '0') > '9')
  454.             *p += ('A' - ('9' + 1));
  455.         ++p;
  456.         } while ((u /= base) > 0);
  457.     if (negative)
  458.         *p++ = '-';
  459.     *p = '\0';            /* terminate the string */
  460.     while (q < --p) {        /* reverse the digits */
  461.         c = *q;
  462.         *q++ = *p;
  463.         *p = c;
  464.         }
  465.     return s;
  466.  
  467.     }
  468.  
  469.  
  470. int htob(str,radix)                /* convert string to integer */
  471.    char *str;
  472.    unsigned int radix;
  473. {
  474.    unsigned int digit, number;
  475.    number=0;
  476.    while (*str) {
  477.       if (isdigit(*str)) digit = *str - '0';
  478.       else if (islower(digit)) digit = *str - 'a' + 10;
  479.       else digit = *str - 'A' + 10;
  480.       number = number * radix + digit;
  481.       str++; }
  482.    return(number);
  483. }
  484.         crlf();
  485. /*
  486.     Print address
  487. */
  488.  
  489.         phex(p,4,2);
  490.         print("=  ");
  491. /*
  492.     Print contents
  493. */
  494.  
  495.         phex(*p,2,2);
  496. /*
  497.     Get